#########################################################################################
####################################  FIGURE A2   #######################################
#########   Women’s Influence and Authority by Group Gender Composition  ################
#########################################################################################

library(here)
library(tidyverse)
library(readxl)
library(glue)
library(gridExtra)
library(dplyr)
library(stats)
library(ggpubr)

#setwd("C:/Users/ganth/Dropbox/StrengthInNumbersReplicationPackage/replicable/figure_a2")
rm(list = ls())
set.seed(12345)

#################################################################### (


##########Figure A2a###############

# Import Study 1 analysis produced in STATA do file
study1_minfl <- read.csv("intermediate_data/study1_groupgenderminfl_labavg_ratio_forgraphing.csv") %>%
  mutate(v1 = factor(v1, levels=c("Majority Male", "Majority Female"))) %>%
  arrange(v1)
study1_linfl <- read.csv("intermediate_data/study1_groupgenderlinfl_labavg_ratio_forgraphing.csv") %>%
  mutate(v1 = factor(v1, levels=c("Majority Male", "Majority Female"))) %>%
  arrange(v1)
study1_minflp <- read_excel("intermediate_data/study1_groupgenderminfl_labavg_ratio_1p.xlsx")
study1_minflp$p <- as.character(study1_minflp$p)
study1_minflp$p[study1_minflp$p == '0'] <- '<0.001'
study1_linflp <- read_excel("intermediate_data/study1_groupgenderlinfl_labavg_ratio_1p.xlsx")
study1_linflp$p <- as.character(study1_linflp$p)
study1_linflp$p[study1_linflp$p == '0'] <- '<0.001'

# Import Study 2 analysis produced in STATA do file
study2_minfl <- read.csv("intermediate_data/study2_groupgenderminfl_ratio_r1_forgraphing.csv") %>%
  mutate(v1 = factor(v1, levels=c("Majority Male", "Majority Female"))) %>%
  arrange(v1)
study2_linfl <- read.csv("intermediate_data/study2_groupgenderlinfl_ratio_r1_forgraphing.csv") %>%
  mutate(v1 = factor(v1, levels=c("Majority Male", "Majority Female"))) %>%
  arrange(v1)
study2_minflp <- read_excel("intermediate_data/study2_groupgenderminfl_ratio_r1_1p.xlsx")
study2_minflp$p <- as.character(study2_minflp$p)
study2_minflp$p[study2_minflp$p == '0'] <- '<0.001'
study2_linflp <- read_excel("intermediate_data/study2_groupgenderlinfl_ratio_r1_1p.xlsx")
study2_linflp$p <- as.character(study2_linflp$p)
study2_linflp$p[study2_linflp$p == '0'] <- '<0.001'

# Import combined analysis produced in STATA do file
combined_minfl <- read.csv("intermediate_data/combined_groupgenderminfl_ratio_ravg_forgraphing.csv") %>%
  mutate(across('v1', str_replace, 'Majority Men', 'Majority Male')) %>%
  mutate(across('v1', str_replace, 'Majority Women', 'Majority Female')) %>%
  mutate(v1 = factor(v1, levels=c("Majority Male", "Majority Female"))) %>%
  arrange(v1)
combined_linfl <- read.csv("intermediate_data/combined_groupgenderlinfl_ratio_ravg_forgraphing.csv") %>%
  mutate(across('v1', str_replace, 'Majority Men', 'Majority Male')) %>%
  mutate(across('v1', str_replace, 'Majority Women', 'Majority Female')) %>%
  mutate(v1 = factor(v1, levels=c("Majority Male", "Majority Female"))) %>%
  arrange(v1)
combined_minflp <- read_excel("intermediate_data/combined_groupgenderminfl_ratio_ravg_1p.xlsx")
combined_minflp$p <- as.character(combined_minflp$p)
combined_minflp$p[combined_minflp$p == '0'] <- '<0.001'
combined_linflp <- read_excel("intermediate_data/combined_groupgenderlinfl_ratio_ravg_1p.xlsx")
combined_linflp$p <- as.character(combined_linflp$p)
combined_linflp$p[combined_linflp$p == '0'] <- '<0.001'

# Initiate figure file
png(filename = "figure_a2a.png", width = 1300, height = 800)
# windowsFonts(A = windowsFont("CMU Serif"))
study1_linflp$y.position <- 2.25
study2_linflp$y.position <- 1.75
combined_linflp$y.position <- 1.75
# Study 1 Least Influential Figure
study1_linflvotes <- ggplot(study1_linfl, aes(x=v1, y=v2), fill="dodgerblue3") +
  geom_bar(data=study1_linfl, stat="identity", aes(x=v1, y=v2), colour="black", fill = c("dodgerblue3", "darkgoldenrod2"), size=.3, width = .6) +
  geom_errorbar(data=study1_linfl, aes(ymin=v2-ci, ymax=v2+ci), linewidth=0.8, width=.1, position=position_dodge(.9)) +
  xlab("Study 1") +
  ylab("Votes per woman") +
  theme(text=element_text(size=20),
        plot.title = element_text(hjust=.5),
        axis.title.y=element_text(size=20),
        axis.title.x=element_text(size=16),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.line = element_line(colour="black")) +
  geom_hline(yintercept = 1, linetype="dashed") +
  coord_cartesian(ylim = c(0, 2.25))
study1_linflvotes <- study1_linflvotes + stat_pvalue_manual(study1_linflp, label = "p={p}",  size=5)

# Study 2 Least Influential Figure
study2_linflvotes <- ggplot(study2_linfl, aes(x=v1, y=v2), fill="dodgerblue3") +
  geom_bar(data=study2_linfl, stat="identity", aes(x=v1, y=v2), colour="black", fill = c("dodgerblue3", "darkgoldenrod2"), size=.3, width = .6) +
  geom_errorbar(data=study2_linfl, aes(ymin=v2-ci, ymax=v2+ci), linewidth=0.8, width=.1, position=position_dodge(.9)) +
  xlab("Study 2") +
  theme(text=element_text(size=20),
        plot.title = element_text(hjust=.5),
        axis.title=element_text(size=16),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        axis.line.x = element_line(colour="black")) +
  geom_hline(yintercept = 1, linetype="dashed") +
  coord_cartesian(ylim = c(0, 2.25))
study2_linflvotes <- study2_linflvotes + stat_pvalue_manual(study2_linflp, label = "p={p}",  size=5)

# Combined Least Influential Figure
combined_linflvotes <- ggplot(combined_linfl, aes(x=v1, y=v2), fill="dodgerblue3") +
  geom_bar(data=combined_linfl, stat="identity", aes(x=v1, y=v2), colour="black", fill = c("dodgerblue3", "darkgoldenrod2"), size=.3, width = .6) +
  geom_errorbar(data=combined_linfl, aes(ymin=v2-ci, ymax=v2+ci), linewidth=0.8, width=.1, position=position_dodge(.9)) +
  xlab("Combined") +
  theme(text=element_text(size=20),
        plot.title = element_text(hjust=.5),
        axis.title=element_text(size=16),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        axis.line.x = element_line(colour="black")) +
  geom_hline(yintercept = 1, linetype="dashed") +
  coord_cartesian(ylim = c(0, 2.25))
combined_linflvotes <- combined_linflvotes + stat_pvalue_manual(combined_linflp, label = "p={p}",  size=5)

# Combine figures and write to file
inflvotes <- grid.arrange(study1_linflvotes, study2_linflvotes, combined_linflvotes, nrow = 1)
inflvotes
dev.off()


####################Figure A2b################

# Import Study 1 analysis produced in STATA do file
study1_minfl <- read.csv("intermediate_data/study1_groupgenderminfl_f2avg_ratio_forgraphing.csv") %>%
  mutate(v1 = factor(v1, levels=c("Majority Male", "Majority Female"))) %>%
  arrange(v1)
study1_linfl <- read.csv("intermediate_data/study1_groupgenderlinfl_f2avg_ratio_forgraphing.csv") %>%
  mutate(v1 = factor(v1, levels=c("Majority Male", "Majority Female"))) %>%
  arrange(v1)
study1_minflp <- read_excel("intermediate_data/study1_groupgenderminfl_f2avg_ratio_4p.xlsx")
study1_minflp$p <- as.character(study1_minflp$p)
study1_minflp$p[study1_minflp$p == '0'] <- '<0.001'
study1_linflp <- read_excel("intermediate_data/study1_groupgenderlinfl_f2avg_ratio_4p.xlsx")
study1_linflp$p <- as.character(study1_linflp$p)
study1_linflp$p[study1_linflp$p == '0'] <- '<0.001'

# Import Study 2 analysis produced in STATA do file
study2_minfl <- read.csv("intermediate_data/study2_groupfemvotesminfl_avgpre_ratio_forgraphing.csv")  %>%
  mutate(v1 = factor(v1, levels=c("Majority Male", "Majority Female"))) %>%
  arrange(v1)
study2_linfl <- read.csv("intermediate_data/study2_groupfemvoteslinfl_avgpre_ratio_forgraphing.csv")  %>%
  mutate(v1 = factor(v1, levels=c("Majority Male", "Majority Female"))) %>%
  arrange(v1)
study2_minflp <- read_excel("intermediate_data/study2_groupfemvotesminfl_avgpre_ratio_4p.xlsx")
study2_minflp$p <- as.character(study2_minflp$p)
study2_minflp$p[study2_minflp$p == '0'] <- '<0.001'
study2_linflp <- read_excel("intermediate_data/study2_groupfemvoteslinfl_avgpre_ratio_4p.xlsx")
study2_linflp$p <- as.character(study2_linflp$p)
study2_linflp$p[study2_linflp$p == '0'] <- '<0.001'

# Import Combined analysis produced in STATA do file
combined_minfl <- read.csv("intermediate_data/combined_groupfemvotesminfl_avgpre_ratio_forgraphing.csv")  %>%
  mutate(v1 = factor(v1, levels=c("Majority Male", "Majority Female"))) %>%
  arrange(v1)
combined_linfl <- read.csv("intermediate_data/combined_groupfemvoteslinfl_avgpre_ratio_forgraphing.csv")  %>%
  mutate(v1 = factor(v1, levels=c("Majority Male", "Majority Female"))) %>%
  arrange(v1)
combined_minflp <- read_excel("intermediate_data/combined_groupfemvotesminfl_avgpre_ratio_4p.xlsx")
combined_minflp$p <- as.character(combined_minflp$p)
combined_minflp$p[combined_minflp$p == '0'] <- '<0.001'
combined_linflp <- read_excel("intermediate_data/combined_groupfemvoteslinfl_avgpre_ratio_4p.xlsx")
combined_linflp$p <- as.character(combined_linflp$p)
combined_linflp$p[combined_linflp$p == '0'] <- '<0.001'

# Initiate figure file
png(filename = "figure_a2b.png", width = 1300, height = 800)

# Study 1 Least Influential Figure
study1_linflvotes <- ggplot(study1_linfl, aes(x=v1, y=v2), fill="dodgerblue3") +
  geom_bar(data=study1_linfl, stat="identity", aes(x=v1, y=v2), colour="black", fill = c("dodgerblue3", "darkgoldenrod2"), size=.3, width = .55) +
  geom_errorbar(data=study1_linfl, aes(ymin=v2-ci, ymax=v2+ci), size=0.8, width=.1, position=position_dodge(.9)) +
  xlab("Study 1") +
  ylab("Votes per woman") +
  theme(text=element_text(size=20),
        plot.title = element_text(hjust=.5),
        axis.title.y=element_text(size=20),
        axis.title.x=element_text(size=16),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.line = element_line(colour="black")) +
  geom_hline(yintercept = 1, linetype="dashed") +
  coord_cartesian(ylim = c(0, 2))
study1_linflvotes <- study1_linflvotes + stat_pvalue_manual(study1_linflp, label = "p={p}", size=5)


# Study 2 Least Influential Figure
study2_linflvotes <- ggplot(study2_linfl, aes(x=v1, y=v2), fill="dodgerblue3") +
  geom_bar(data=study2_linfl, stat="identity", aes(x=v1, y=v2), colour="black", fill = c("dodgerblue3", "darkgoldenrod2"), size=.3, width = .55) +
  geom_errorbar(data=study2_linfl, aes(ymin=v2-ci, ymax=v2+ci), size=0.8, width=.1, position=position_dodge(.9)) +
  xlab("Study 2") +
  theme(text=element_text(size=20),
        plot.title = element_text(hjust=.5),
        axis.title.x=element_text(size=16),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        axis.line.x = element_line(colour="black")) +
  geom_hline(yintercept = 1, linetype="dashed") +
  coord_cartesian(ylim = c(0, 2))
study2_linflvotes <- study2_linflvotes + stat_pvalue_manual(study2_linflp, label = "p={p}", size=5)

# Combined Least Influential Figure
combined_linflvotes <- ggplot(combined_linfl, aes(x=v1, y=v2), fill="dodgerblue3") +
  geom_bar(data=combined_linfl, stat="identity", aes(x=v1, y=v2), colour="black", fill = c("dodgerblue3", "darkgoldenrod2"), size=.3, width = .55) +
  geom_errorbar(data=combined_linfl, aes(ymin=v2-ci, ymax=v2+ci), size=0.8, width=.1, position=position_dodge(.9)) +
  xlab("Combined") +
  theme(text=element_text(size=20),
        plot.title = element_text(hjust=.5),
        axis.title.x=element_text(size=16),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        axis.line.x = element_line(colour="black")) +
  geom_hline(yintercept = 1, linetype="dashed") +
  coord_cartesian(ylim = c(0, 2))
combined_linflvotes <- combined_linflvotes + stat_pvalue_manual(combined_linflp, label = "p={p}", size=5)

# Combine figures and write to file
inflvotes <- grid.arrange(study1_linflvotes, study2_linflvotes, combined_linflvotes, nrow = 1)
inflvotes
dev.off()


###############Figure A2c#####################
study1_msuppc <- read.csv("intermediate_data/study1_groupgendermsupp_labavg_ratio_forgraphing.csv") %>%
  mutate(across('v1', str_replace, 'Majority Men', 'Majority Male')) %>%
  mutate(across('v1', str_replace, 'Majority Women', 'Majority Female')) %>%
  mutate(v1 = factor(v1, levels=c("Majority Male", "Majority Female"))) %>%
  arrange(v1)
study1_msuppcp <- read_excel("intermediate_data/study1_groupgendermsupp_labavg_ratio_1p.xlsx")
study1_msuppcp$p <- as.character(study1_msuppcp$p)
study1_msuppcp$p[study1_msuppcp$p == '0'] <- '<0.001'

# Import Study 2 analysis produced in STATA do file
study2_msuppc <- read.csv("intermediate_data/study2_groupgendermsupp_ravg_ratio_forgraphing.csv") %>%
  arrange(v1) %>%
  mutate(across('v1', str_replace, 'Majority Men', 'Majority Male')) %>%
  mutate(across('v1', str_replace, 'Majority Women', 'Majority Female')) %>%
  mutate(v1 = factor(v1, levels=c("Majority Male", "Majority Female"))) %>%
  arrange(v1)
study2_msuppcp <- read_excel("intermediate_data/study2_groupgendermsupp_ravg_ratio_1p.xlsx")
study2_msuppcp$p <- as.character(study2_msuppcp$p)
study2_msuppcp$p[study2_msuppcp$p == '0'] <- '<0.001'

# Import Combined analysis produced in STATA do file
combined_msuppc <- read.csv("intermediate_data/combined_groupgendermsupp_ravg_ratio_forgraphing.csv") %>%
  arrange(v1) %>%
  mutate(across('v1', str_replace, 'Majority Men', 'Majority Male')) %>%
  mutate(across('v1', str_replace, 'Majority Women', 'Majority Female')) %>%
  mutate(v1 = factor(v1, levels=c("Majority Male", "Majority Female"))) %>%
  arrange(v1)
combined_msuppcp <- read_excel("intermediate_data/combined_groupgendermsupp_ravg_ratio_1p.xlsx")
combined_msuppcp$p <- as.character(combined_msuppcp$p)
combined_msuppcp$p[combined_msuppcp$p == '0'] <- '<0.001'

# Initiate figure file
png(filename = "figure_a2c.png", width = 1300, height = 800)

# Study 1 Figure
study1_msuppfigc <- ggplot(study1_msuppc, aes(x=v1, y=v2), fill="dodgerblue3") +
  geom_bar(position=position_dodge(), stat="identity", colour="black", fill = c("dodgerblue3", "darkgoldenrod2"), size=.1, width = .6) +
  geom_errorbar(aes(ymin=v2-ci, ymax=v2+ci), size=0.8, width=.1, position=position_dodge(.9)) +
  xlab("Study 1") +
  ylab("Votes per woman") +
  theme(text=element_text(size=20),
        plot.title = element_text(hjust=.5),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.line = element_line(colour="black")) +
  geom_hline(yintercept = 1, linetype = "dashed") +
  coord_cartesian(ylim = c(0, 1.5))
study1_msuppfigc <- study1_msuppfigc + stat_pvalue_manual(study1_msuppcp, label = "p={p}", size=5)


# Study 2 Figure
study2_msuppfigc <- ggplot(study2_msuppc, aes(x=v1, y=v2), fill="dodgerblue3") +
  geom_bar(position=position_dodge(), stat="identity", colour="black", fill = c("dodgerblue3", "darkgoldenrod2"), size=.07, width = .6) +
  geom_errorbar(aes(ymin=v2-ci, ymax=v2+ci), size=0.8, width=.1, position=position_dodge(.9)) +
  xlab("Study 2") +
  theme(text=element_text(size=20),
        plot.title = element_text(hjust=.5),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        axis.line = element_line(colour="black")) +
  geom_hline(yintercept = 1, linetype = "dashed") +
  coord_cartesian(ylim = c(0, 1.5))
study2_msuppfigc <- study2_msuppfigc + stat_pvalue_manual(study2_msuppcp, label = "p={p}0", size=5)

# Combined Figure
combined_msuppfigc <- ggplot(combined_msuppc, aes(x=v1, y=v2), fill="dodgerblue3") +
  geom_bar(position=position_dodge(), stat="identity", colour="black", fill = c("dodgerblue3", "darkgoldenrod2"), size=.07, width = .6) +
  geom_errorbar(aes(ymin=v2-ci, ymax=v2+ci), size=0.8, width=.1, position=position_dodge(.9)) +
  xlab("Combined") +
  theme(text=element_text(size=20),
        plot.title = element_text(hjust=.5),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        axis.line = element_line(colour="black")) +
  geom_hline(yintercept = 1, linetype = "dashed") +
  coord_cartesian(ylim = c(0, 1.5))
combined_msuppfigc <- combined_msuppfigc + stat_pvalue_manual(combined_msuppcp, label = "p={p}", size=5)

# Combine figures and write to file
msuppfigc <- grid.arrange(study1_msuppfigc, study2_msuppfigc, combined_msuppfigc, nrow = 1)
msuppfigc
dev.off()
